<template>
    <div>
        <div class="operate center-flex-box" v-if="hasPermission('PAGE_ALL_ROLE')">
            <el-button class="item" type="primary" v-if="hasPermission('ADD_ROLE')" @click="showEditor(null)">新增
            </el-button>
            <el-input placeholder="请输入内容" v-model="queryInfo.content" class="input-with-select item">
                <el-select v-model="queryInfo.mode" clearable slot="prepend" placeholder="请选择">
                    <el-option label="ID" value="ids"></el-option>
                    <el-option label="名称" value="names"></el-option>
                </el-select>
            </el-input>
            <div class="item center-flex-box">
                <el-checkbox v-model="queryInfo.checked">筛选状态</el-checkbox>
                <el-checkbox v-model="queryInfo.enable" :disabled="!queryInfo.checked">启用</el-checkbox>
            </div>
            <el-button class="item" @click="obtainRoles">查找</el-button>
        </div>
        <el-table
            v-if="hasPermission('PAGE_ALL_ROLE')"
            :data="roles"
            stripe
            max-height="532"
            style="width: 100%">
            <el-table-column
                prop="id"
                label="ID"
                sortable
                width="180">
            </el-table-column>
            <el-table-column
                prop="name"
                label="名称"
                sortable
                width="120">
            </el-table-column>
            <el-table-column
                prop="description"
                label="描述"
                width="400">
            </el-table-column>
            <el-table-column
                label="状态"
                width="120">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.enable" type="success">启用</el-tag>
                    <el-tag v-else type="danger">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                v-if="hasPermission('UPDATE_ROLE')||hasPermission('DELETE_ROLE')"
                fixed="right"
                width="180"
                label="操作">
                <template slot-scope="scope">
                    <el-button @click="showEditor(scope.row.id)"
                               v-if="hasPermission('UPDATE_ROLE')"
                               type="primary"
                               size="mini"
                               plain>
                        编辑
                    </el-button>
                    <el-button @click="remove(scope.row.id)"
                               v-if="hasPermission('DELETE_ROLE')"
                               type="danger"
                               size="mini"
                               plain>
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog :title="operate==='insert'?'新增角色':'修改角色'" :visible.sync="editor" width="40%">
            <el-form ref="role" :rules="rules" :model="role" label-position="left" label-width="80px" size="small">
                <el-form-item label="ID">
                    <el-input v-model="role.id" disabled></el-input>
                </el-form-item>
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="role.name"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input type="textarea" v-model="role.description"></el-input>
                </el-form-item>
                <el-form-item label="是否启用">
                    <div class="enable">
                        <el-switch v-model="role.enable"></el-switch>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editor = false">取 消</el-button>
                <el-button type="primary" @click="commit">提交</el-button>
            </div>
        </el-dialog>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageInfo.page"
            :page-size="pageInfo.size"
            layout="total, prev, pager, next, jumper"
            :total="pageInfo.total">
        </el-pagination>
    </div>
</template>

<script>
    import { mapGetters, mapActions } from 'vuex'

    export default {
        name: 'roleManagement',
        data () {
            return {
                roles: [],
                role: {
                    id: '',
                    enable: false
                },
                pageInfo: {
                    page: 1,
                    size: 100,
                    total: 0
                },
                queryInfo: {
                    mode: '',
                    content: '',
                    checked: false,
                    enable: false
                },
                editor: false,
                operate: 'insert',
                rules: {
                    name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
                }
            }
        },
        mounted () {
            this.obtainRoles()
        },
        computed: {
            ...mapGetters({
                hasPermission: 'user/hasPermission'
            })
        },
        methods: {
            ...mapActions({
                pageRoleInfoAction: 'user/pageRoleInfo',
                insertRoleAction: 'user/insertRole',
                updateRoleAction: 'user/updateRole',
                deleteRoleAction: 'user/deleteRole'
            }),
            handleSizeChange (val) {
                this.pageInfo.size = val
                this.obtainRoles()
            },
            handleCurrentChange (val) {
                this.pageInfo.page = val
                this.obtainRoles()
            },
            obtainRoles () {
                this.action(this.pageRoleInfoAction(this.obtainQuery()), data => {
                    if (data.data) {
                        this.roles = data.data.records
                        this.pageInfo.total = data.data.total
                    } else {
                        this.roles = []
                    }
                })
            },
            obtainQuery () {
                let query = {
                    page: this.pageInfo.page,
                    size: this.pageInfo.size,
                    enable: this.queryInfo.checked ? this.queryInfo.enable : null
                }
                query[this.queryInfo.mode] = this.queryInfo.content
                return query
            },
            showEditor (id) {
                if (id) {
                    this.role = this.roles.find(role => role.id === id)
                    this.operate = 'update'
                } else {
                    this.role = { id: 'ID会在添加时自动生成', enable: false }
                    this.operate = 'insert'
                }
                this.editor = true
            },
            commit () {
                this.$refs['role'].validate((valid) => {
                    if (valid) {
                        if (this.operate === 'insert') {
                            this.action(this.insertRoleAction(this.role), data => {
                                this.$message.success('添加成功')
                                this.editor = false
                                this.obtainRoles()
                            })
                        } else if (this.operate === 'update') {
                            this.action(this.updateRoleAction(this.role), data => {
                                this.$message.success('更新成功')
                                this.editor = false
                                this.obtainRoles()
                            })
                        }
                    } else {
                        return false
                    }
                })
            },
            remove (id) {
                this.action(this.deleteRoleAction(id), data => {
                    this.$message.success('删除成功')
                    this.obtainRoles()
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import '../../../../../style/scss';

    .operate {
        height: 60px;
        width: 90%;
        padding: 0 5%;

        .item {
            margin: 24px;
        }

        .input-with-select {
            width: 360px;
            background-color: #fff;

            .el-select {
                width: 100px;
            }
        }
    }

    .enable {
        width: 15%;
    }
</style>
